package com.tebiecloud.yunst.server.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tebiecloud.yunst.client.model.entity.YunBankCard;
import com.tebiecloud.yunst.server.service.YunBankCardService;
import com.tebiecloud.common.model.PageParams;
import com.tebiecloud.common.model.ResultBody;
import com.tebiecloud.common.utils.ResultBodyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
import java.util.Map;

/**
 * 用户绑定的银行卡 前端控制器
 * @author tebie
 * @date 2019-09-17
 */
@RestController
@RequestMapping("/yunbankcard")
public class YunBankCardController {

    @Autowired
    private YunBankCardService yunBankCardService;

    /**
     * 获取分页数据
     * @param map
     * @return
     */
    @GetMapping(value = "list")
    public ResultBody<IPage<YunBankCard>> list(@RequestParam(required = false) Map map) {
        PageParams pageParams = new PageParams(map);
        YunBankCard query = pageParams.mapToObject(YunBankCard.class);
        QueryWrapper<YunBankCard> queryWrapper = new QueryWrapper();
        return ResultBodyUtils.ok(yunBankCardService.page(new PageParams(map), queryWrapper));
    }

    /**
     * 根据ID查找数据
     * @param id
     * @return
     */
    @GetMapping("get")
    public ResultBody<YunBankCard> get(@RequestParam("id") Long id) {
        YunBankCard entity = yunBankCardService.getById(id);
        return ResultBodyUtils.ok(entity);
    }

    /**
     * 添加数据
     * @param userId 用户id,
     * @param bizUserId 云商通用户id,
     * @param cardNo 卡号,
     * @param identityNo 身份证号,
     * @param name 名称,
     * @param phone 绑定手机号,
     * @param bank 银行名称,
     * @param type 银行卡类型名称,
     * @param remark 备注,
     * @param status 状态：0禁用，1启用,
     * @param icon 图标
     * @return
     */
    @PostMapping("add")
    public ResultBody add(
        @RequestParam(value = "userId", required = false) Long userId,
        @RequestParam(value = "bizUserId", required = false) String bizUserId,
        @RequestParam(value = "cardNo", required = false) String cardNo,
        @RequestParam(value = "identityNo", required = false) String identityNo,
        @RequestParam(value = "name", required = false) String name,
        @RequestParam(value = "phone", required = false) String phone,
        @RequestParam(value = "bank", required = false) String bank,
        @RequestParam(value = "type", required = false) String type,
        @RequestParam(value = "remark", required = false) String remark,
        @RequestParam(value = "status", required = false) Integer status,
        @RequestParam(value = "icon", required = false) String icon
    ) {
        YunBankCard entity = new YunBankCard();
        entity.setUserId(userId);
        entity.setBizUserId(bizUserId);
        entity.setCardNo(cardNo);
        entity.setIdentityNo(identityNo);
        entity.setName(name);
        entity.setPhone(phone);
        entity.setBank(bank);
        entity.setType(type);
        entity.setRemark(remark);
        entity.setStatus(status);
        entity.setIcon(icon);
        yunBankCardService.save(entity);
        return ResultBodyUtils.ok();
    }

    /**
     * 更新数据
     * @param userId 用户id
     * @param bizUserId 云商通用户id
     * @param cardNo 卡号
     * @param identityNo 身份证号
     * @param name 名称
     * @param phone 绑定手机号
     * @param bank 银行名称
     * @param type 银行卡类型名称
     * @param remark 备注
     * @param status 状态：0禁用，1启用
     * @param icon 图标
     * @return
     */
    @PostMapping("update")
    public ResultBody update(
            @RequestParam(value = "bankCardId", required = false) Long bankCardId,
            @RequestParam(value = "userId", required = false) Long userId,
            @RequestParam(value = "bizUserId", required = false) String bizUserId,
            @RequestParam(value = "cardNo", required = false) String cardNo,
            @RequestParam(value = "identityNo", required = false) String identityNo,
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "phone", required = false) String phone,
            @RequestParam(value = "bank", required = false) String bank,
            @RequestParam(value = "type", required = false) String type,
            @RequestParam(value = "remark", required = false) String remark,
            @RequestParam(value = "status", required = false) Integer status,
            @RequestParam(value = "icon", required = false) String icon
    ) {
        YunBankCard entity = new YunBankCard();
        entity.setBankCardId(bankCardId);
        entity.setUserId(userId);
        entity.setBizUserId(bizUserId);
        entity.setCardNo(cardNo);
        entity.setIdentityNo(identityNo);
        entity.setName(name);
        entity.setPhone(phone);
        entity.setBank(bank);
        entity.setType(type);
        entity.setRemark(remark);
        entity.setStatus(status);
        entity.setIcon(icon);
        yunBankCardService.updateById(entity);
        return ResultBodyUtils.ok();
    }

    /**
     * 删除数据
     * @param id 单个id
     * @param ids 多个id用,号隔开
     * @return
     */
    @PostMapping("remove")
    public ResultBody remove(
            @RequestParam(value = "id", required = false) Long id,
            @RequestParam(value = "ids", required = false) String ids
    ) {
        if(id != null && id > 0) {
            yunBankCardService.removeById(id);
        }
        if(ids != null && ids.length() > 0) {
            yunBankCardService.removeByIds(Arrays.asList(ids.split(",")));
        }
        return ResultBodyUtils.ok();
    }

}
